/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.hazardsource.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.hazardsource.domain.IndicationItems;
import com.koron.hazardsource.service.api.IndicationItemsService;
import com.koron.hazardsource.util.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Date;

@Api(tags = "indicationItems",description = "指标项相关接口")
@RestController
@RequestMapping("/indicationItems")
public class IndicationItemsController {

    @Autowired
    private IndicationItemsService itemsService;

    @ApiOperation("保存指标项")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "指标项id,新增不传，修改必传",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="name",value = "指标项名称",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="typeKey",value = "指标类型key",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="indicationIndex",value = "指标项排序index",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name="remark",value = "备注",dataType = "String",paramType = "query"),
    })
    @RequestMapping(value = "/save.htm",method = RequestMethod.POST)
    public MessageBean<Integer> save(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore IndicationItems indicationItems){
        if (null==user){
            MessageBean<Integer> msg=new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        indicationItems.setUpdateTime(new Date());
        indicationItems.setUpdateUser(user.getName());
        return itemsService.save(indicationItems);
    }

    @ApiOperation("删除指标类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ids",value = "指标类型id,多个之间用，分开",required = true,dataType = "String",paramType = "query")
    })
    @RequestMapping(value = "/batchDelete.htm",method = RequestMethod.POST)
    public MessageBean<Integer> batchDelete(@RequestParam final String ids){
        return itemsService.batchDelete(StringUtils.isEmpty(ids)?null: Arrays.asList(ids.split("[,，]")));
    }

    @ApiOperation("指标类型详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "指标类型id",required = true,dataType = "String",paramType = "query")
    })
    @RequestMapping(value = "/details.htm",method = RequestMethod.POST)
    public MessageBean<IndicationItems> details(@RequestParam final String id){

        return itemsService.queryOne(id);
    }

    @ApiOperation("根据指标类型key查询指标项")
    @ApiImplicitParams({
            @ApiImplicitParam(name="typeKey",value = "指标类型key",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="name",value = "指标项名称",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="page",value = "当前页",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name="pageSize",value = "每页条数",dataType = "int",paramType = "query")
    })
    @RequestMapping(value = "/list.htm",method = RequestMethod.POST)
    public MessageBean<DataBean<IndicationItems>> list(@RequestParam(required = false) final String typeKey,@RequestParam(required = false) final
    String name,@RequestParam(required = false)  Integer page, @RequestParam(required = false) Integer pageSize){
        Integer start=null;
        if (page!=null){
            if (page<=0){
                page=1;
            }
            if (pageSize==null||pageSize<=0){
                pageSize=15;
            }
            start=(page<=1)?0:(page-1)*pageSize;
        }
        MessageBean<DataBean<IndicationItems>> msg=itemsService.queryByCondition(typeKey, name, start, pageSize);
        msg.getData().setPageNo(page);
        return msg;
    }
}
